package com.tjc.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tjc.common.CommonService;
import com.tjc.model.entity.InterfaceInfo;
import com.tjc.project.common.ErrorCode;
import com.tjc.project.exception.BusinessException;
import com.tjc.project.model.entity.User;
import com.tjc.project.model.entity.UserInterfaceInfo;
import com.tjc.project.service.InterfaceInfoService;
import com.tjc.project.service.UserInterfaceInfoService;
import com.tjc.project.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @ClassName ApiCommonServiceImpl
 * @Author tjc
 **/
@DubboService
public class ApiCommonServiceImpl implements CommonService {

    @Resource
    UserInterfaceInfoService userInterfaceInfoService;

    @Resource
    UserService userService;

    @Resource
    InterfaceInfoService interfaceInfoService;

    /**
     * 这个是rpc调用的时候的方法
     *
     * @param interfaceId 接口的id
     * @return
     */
    @Override
    public Boolean invokeCount(Long userId, Long interfaceId) {
        if (userId <= 0 || interfaceId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // 创建查询wrapper
        QueryWrapper<UserInterfaceInfo> wrapper =
                new QueryWrapper<UserInterfaceInfo>().
                        eq("userId", userId).
                        eq("interfaceInfoId", interfaceId);

        // 首先判断接口是否还存在同时是否还在开启
        UserInterfaceInfo byId = userInterfaceInfoService.getOne(wrapper);
        if (byId == null || byId.getStatus() != 0) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "您访问的接口飞走了！");
        }

        // 对调用次数进行校验
        if (byId.getLeftNum() <= 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"您的访问次数没有了！");
        }

        // 将接口中的次数进行修改就可以了
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.
                eq("userId", userId).
                eq("interfaceInfoId", interfaceId).
                setSql("leftNum = leftNum - 1,totalNum = totalNum + 1");

        return userInterfaceInfoService.update(updateWrapper);
    }

    @Override
    public String getSecretKey(String accessKey) {
        // 判断空或者存在或者不存在
        if (accessKey == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("accessKey", accessKey);
        User user = userService.getOne(wrapper);

        // 判断是否存在
        if (user == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "您访问的用户不存在！");
        }

        // 判断是否存在sk
        String secretKey = user.getSecretKey();
        if (secretKey == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "sk不存在，请检查！");
        }

        return secretKey;
    }

    @Override
    public com.tjc.model.entity.User getUser(String accessKey) {
        com.tjc.model.entity.User use = new com.tjc.model.entity.User();

        if (accessKey == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("accessKey", accessKey);
        User user = userService.getOne(wrapper);

        if (user == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        BeanUtils.copyProperties(user, use);

        return use;
    }

    /**
     * 这个方法通过请求的方式和url得到interface详细信息
     *
     * @param path   接口url
     * @param method 接口请求方法
     * @return 接口
     */
    @Override
    public InterfaceInfo getInterfaceInfo(String path, String method) {
        if (StringUtils.isEmpty(path) || StringUtils.isEmpty(method)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // wrapper规定参数进行查询
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        QueryWrapper<com.tjc.project.model.entity.InterfaceInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("url", path).eq("method", method);

        // 从数据库中查询interface
        com.tjc.project.model.entity.InterfaceInfo interfaceInfo1 = interfaceInfoService.getOne(wrapper);
        if (interfaceInfo1 == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        // 将参数写入
        BeanUtils.copyProperties(interfaceInfo1, interfaceInfo);

        return interfaceInfo;
    }

}
